package com.returnsoft.resource.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.AreaDto;
import com.returnsoft.resource.dto.TrainerDto;
import com.returnsoft.resource.eao.AreaEao;
import com.returnsoft.resource.eao.TrainerEao;
import com.returnsoft.resource.entity.Area;
import com.returnsoft.resource.entity.Trainer;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class TrainerBean
 */
@Stateless
@LocalBean
public class TrainerBean implements TrainerInterface {

	@EJB
	private TrainerEao trainerEao;

	@EJB
	private AreaEao areaEao;

	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public TrainerBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<AreaDto> findAreas(Integer trainerId)
			throws RecruitmentException {
		try {
			List<Area> areas = areaEao.findByTrainer(trainerId);
			List<AreaDto> areasDto = new ArrayList<AreaDto>();
			for (Area area : areas) {
				AreaDto areaDto = conv.fromArea(area);
				areasDto.add(areaDto);
			}
			return areasDto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public TrainerDto findByUser(Integer userId) throws RecruitmentException {
		try {

			Trainer trainer = trainerEao.findByUserId(userId);
			if (trainer!=null && trainer.getId()>0) {
				TrainerDto trainerDto = conv.fromTrainer(trainer);
				List<Area> areas = areaEao.findByTrainer(trainer.getId());

				List<AreaDto> areasDto = new ArrayList<AreaDto>();

				for (Area area : areas) {
					AreaDto areaDto = conv.fromArea(area);
					areasDto.add(areaDto);

				}

				trainerDto.setAreasAvailable(areasDto);
				return trainerDto;
			}else{
				return null;
			}
			

			

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public TrainerDto findById(Integer trainerId) throws RecruitmentException {
		try {

			Trainer trainer = trainerEao.findById(trainerId);
			TrainerDto trainerDto = conv.fromTrainer(trainer);

			List<Area> areas = areaEao.findByTrainer(trainer.getId());

			List<AreaDto> areasDto = new ArrayList<AreaDto>();

			for (Area area : areas) {
				AreaDto areaDto = conv.fromArea(area);
				areasDto.add(areaDto);

			}

			trainerDto.setAreasAvailable(areasDto);

			return trainerDto;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public TrainerDto add(TrainerDto trainerDto) throws RecruitmentException {
		try {

			Trainer trainer = conv.toTrainer(trainerDto);
			System.out.println("en el bean");

			// System.out.println(trainer.getDepartments().size());

			trainer = trainerEao.add(trainer);

			trainerDto = conv.fromTrainer(trainer);

			return trainerDto;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public List<TrainerDto> findList(List<Integer> areasId, Boolean areaNull,
			String name, String documentNumber) throws RecruitmentException {
		try {

			List<TrainerDto> entities = trainerEao.findList(areasId, areaNull,
					name, documentNumber);

			return entities;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public List<TrainerDto> findAll() throws RecruitmentException {
		try {

			List<TrainerDto> entities = trainerEao.findAll();

			return entities;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

}
